package top.honlnk.service.impl.processor;


import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import top.honlnk.common.constant.UserConstant;
import top.honlnk.common.exception.CustomException;

@Slf4j
public class ImageDataParserProcessor {

    public String[][] ProcessorData(JSONObject jsonObject) {
        JSONArray body = extractBody(jsonObject);
        JSONArray filteredBody = filterJsonData(body);

        int numRows = 13;
        int numCols = 8;
        String[][] array = storeDataInArray(filteredBody, numRows, numCols);
        String[][] table = condenseArray(array);
        return adaptLakeIdentify(table);
    }

    private static JSONArray extractBody(JSONObject jsonObject) {
        try {
            JSONArray jsonArray = jsonObject.getJSONArray("tables_result");
            JSONObject firstObject = jsonArray.getJSONObject(0);
            return firstObject.getJSONArray("body");
        } catch (Exception e) {
            log.error(UserConstant.RESPONSE_BODY_IS_NULL, e.getMessage());
            throw new RuntimeException();
        }
    }

    private static JSONArray filterJsonData(JSONArray body) {
        JSONArray filteredBody = new JSONArray();

        for (int i = 0; i < body.length(); i++) {
            try {
                JSONObject rowJson = body.getJSONObject(i);
                JSONObject filteredRowJson = new JSONObject();

                // 筛选所需属性
                String[] desiredKeys = {"col_end", "row_end", "words", "row_start", "col_start"};
                for (String key : desiredKeys) {
                    if (rowJson.has(key)) {
                        filteredRowJson.put(key, rowJson.get(key));
                    }
                }

                filteredBody.put(filteredRowJson);
            } catch (JSONException e) {
                throw new CustomException(e.getMessage());
            }
        }

        return filteredBody;
    }

    private static String[][] storeDataInArray(JSONArray filteredBody, int numRows, int numCols) {
        String[][] array = new String[numRows][numCols];

        for (int i = 0; i < filteredBody.length(); i++) {
            try {
                JSONObject data = filteredBody.getJSONObject(i);
                int rowStart = data.getInt("row_start");
                int colStart = data.getInt("col_start");
                int rowEnd = data.getInt("row_end");
//            int colEnd = data.getInt("col_end");
                String word = data.getString("words");

                // 确保数据位置在数组范围内
                if (rowStart >= 0 && rowStart < numRows && colStart >= 0 && colStart < numCols) {
                    for (int j = 0; j <= rowEnd - rowStart; j++) {
                        if (rowStart + j < 13) {
//                            if (rowStart == 0 || colStart == 0) {
//                                array[rowStart + j][colStart] = "有课";
//                            } else
                            if (word.isEmpty()) {
                                array[rowStart + j][colStart] = "";
                            } else {
                                array[rowStart + j][colStart] = "有课";
                            }
                        }
                    }
                }
            } catch (JSONException e) {
                throw new CustomException(e.getMessage());
            }
        }
        return array;
    }

    private static String[][] condenseArray(String[][] originalArray) {
        int numRows = 5;
        int numCols = 5;
        String[][] condensedArray = new String[numRows][numCols];

        int condensedArrayRow = 0;
        int condensedArrayCol = 0;
        for (int i = 1; i < originalArray.length - 2; i += 2) {
            for (int j = 1; j < originalArray[i].length - 2; j++) {
                condensedArray[condensedArrayRow][condensedArrayCol] = originalArray[i][j];
                condensedArrayCol++;
            }
            condensedArrayRow++;
            condensedArrayCol = 0;
        }

        return condensedArray;
    }

    private static String[][] adaptLakeIdentify(String[][] originalArray) {
        // 检索以西数组中有没有值为null的数据，如果有的话将其修改为空字符串
        for (int i = 0; i < originalArray.length; i++) {
            for (int j = 0; j < originalArray[i].length; j++) {
                if (originalArray[i][j] == null) {
                    originalArray[i][j] = "";
                }
            }
        }
        return originalArray;
    }
}
